package com.cloudbroker.bcs.platform.dav.common.util;

import java.util.Map;
import java.util.Set;

import com.cloudbroker.bcs.common.cache.CacheClient;
import com.cloudbroker.bcs.common.cache.CacheClientFactory;
import com.cloudbroker.bcs.common.cache.redis.RedisClient;

public class CacheHelper extends com.cloudbroker.bcs.common.util.CacheHelper {
    
    private static CacheHelper instance = new CacheHelper();

    private CacheClient cacheClient;
    
    public static CacheHelper getInstance() {
        return instance;
    }
    
    @Override
    public CacheClient getCacheClient() {
        if (null == cacheClient) {
            cacheClient = CacheClientFactory.createCacheClientFromConfigUtil();
        }
        return cacheClient;
    }
    
    public long sdd(String key, String[] values) {
        if (getCacheClient() instanceof RedisClient) {
            return ((RedisClient) getCacheClient()).sdd(key, values);
        }
        return 0l;
    }

    public long sdd(String key, String member) {
        if (getCacheClient() instanceof RedisClient) {
            return ((RedisClient) getCacheClient()).sdd(key, member);
        }
        return 0l;
    }

    public boolean sismember(String key, String member) {
        if (getCacheClient() instanceof  RedisClient) {
            return ((RedisClient)getCacheClient()).sismember(key, member);
        }
        return false;
    }

    public long scard(String key) {
        if (getCacheClient() instanceof  RedisClient) {
            return ((RedisClient)getCacheClient()).scard(key);
        }
        return 0l;
    }
    
    public Set<String> smembers(String key) {
        if (getCacheClient() instanceof RedisClient) {
            return ((RedisClient) getCacheClient()).smembers(key);
        }
        return null;
    }
    
    public void sremove(String key, String members) {
        if (getCacheClient() instanceof RedisClient) {
            ((RedisClient) getCacheClient()).srem(key, members);
        }
    }
    
    public static long zadd(String key, double score, String member) {
        return getInstance().getCacheClient().zadd(key, score, member);
    }
    
    public static long zrem(String key, double score, String member) {
        return getInstance().getCacheClient().zrem(key, score, member);
    }
    
    public static long zcard(String key) {
        return getInstance().getCacheClient().zcard(key);
    }

    public static Set<String> zrange(String key,long start,long end){
        return getInstance().getCacheClient().zrange(key , start , end);
    }

    public static Long ttl(String key){
        return getInstance().getCacheClient().ttl(key);
    }
    
    public static Long setnx(String key, String value){
        return getInstance().getCacheClient().setnx(key, value);
    }

    public static Long setnx(String key, String value, int expireTime){
        return getInstance().getCacheClient().setnx(key, value, expireTime);
    }

    public static boolean expire(String key,int seconds){
        return getInstance().getCacheClient().expire(key, seconds);
    }

    public long hset(String key, String field, String value) {
        if (getCacheClient() instanceof RedisClient) {
            return ((RedisClient) getCacheClient()).hset(key, field, value);
        }
        return 0l;
    }

    public String hmset(String key, Map<String, String> hash) {
        if (getCacheClient() instanceof RedisClient) {
            return ((RedisClient) getCacheClient()).hmset(key, hash);
        }
        return null;
    }

    public String hget(String key, String field) {
        if (getCacheClient() instanceof RedisClient) {
            return ((RedisClient) getCacheClient()).hget(key, field);
        }
        return null;
    }

    public Long hincrBy(String key, String field, Long value) {
        if (getCacheClient() instanceof RedisClient) {
            return ((RedisClient) getCacheClient()).hincrBy(key, field, value);
        }
        return 0l;
    }

    public boolean hdel(String key, String... field) {
        if (getCacheClient() instanceof RedisClient) {
            return ((RedisClient) getCacheClient()).hdel(key, field);
        }
        return false;
    }
}
